package server.connector;
import java.net.*;
import server.dbconnector.*;
import java.io.*;
import helper.request.*;
import helper.request.logandreg.*;
import helper.request.contacts.*;
import helper.request.usermgmt.*;
import helper.response.*;
public class ServerConnectorThread implements Runnable{
    private Socket sock;
    private DatabaseConnector dc;
    public ServerConnectorThread(Socket s,DatabaseConnector dc){
    	this.sock=s;
    	this.dc=dc;
    }
	public void run(){
		try {
			ObjectInputStream in=new ObjectInputStream(sock.getInputStream());
			Request req=(Request)in.readObject();
			if(req.getClass().getName().contains("SearchUserRequest")){
				SearchUserRequest sur=(SearchUserRequest)req;
				AdminandSearchResponse asr=sur.process(dc,111);
				ObjectOutputStream out=new ObjectOutputStream(sock.getOutputStream());
				out.writeObject(asr);
			}
			else if(req.getClass().getName().contains("AdminRequest")){
			    AdminRequest ar=(AdminRequest)req;
			    AdminandSearchResponse asr=ar.process(dc,111);
			    ObjectOutputStream out=new ObjectOutputStream(sock.getOutputStream());
				out.writeObject(asr);
			}
			else if(req.getClass().getName().contains("AdminRegisterRequest")){
				AdminRegisterRequest arr=(AdminRegisterRequest)req;
				AdminandSearchResponse asr=arr.process(dc,111);
			    ObjectOutputStream out=new ObjectOutputStream(sock.getOutputStream());
				out.writeObject(asr);
			}
			else if(req.getClass().getName().contains("LoginRequest")){
				LoginRequest lr=(LoginRequest)req;
				LoginResponse lrr=lr.process(dc,111);
				ObjectOutputStream out=new ObjectOutputStream(sock.getOutputStream());
				out.writeObject(lrr);
			}
			else if(req.getClass().getName().contains("RegisterRequest")){
				RegisterRequest rr=(RegisterRequest)req;
				RegResponse rrr=rr.process(dc,1);
				ObjectOutputStream out=new ObjectOutputStream(sock.getOutputStream());
				out.writeObject(rrr);
			}
			else {
				req.process(dc);
				ObjectOutputStream out=new ObjectOutputStream(sock.getOutputStream());
				out.writeObject(new RegResponse(true,null));
			}
		} catch (Exception ex) {
			System.err.println(ex.getMessage());
			ex.printStackTrace();
		}
	}

}
